package service

import (
	"context"
	"errors"
	"fmt"
	"gitee.com/chunanyong/zorm"
	"gitee.com/xls-web/zorm-gen/test/code/structs"
)

// SavePeopleStruct 保存
// 如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
// 如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
// 如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
// 但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func SavePeopleStruct(ctx context.Context, peoplestruct *structs.PeopleStruct) error {

	// peoplestruct对象指针不能为空
	if peoplestruct == nil {
		return errors.New("peoplestruct对象指针不能为空")
	}
	//匿名函数return的error如果不为nil,事务就会回滚
	_, errSavePeopleStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始

		_, errSavePeopleStruct := zorm.Insert(ctx, peoplestruct)

		if errSavePeopleStruct != nil {
			return nil, errSavePeopleStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errSavePeopleStruct != nil {
		errSavePeopleStruct := fmt.Errorf("service.SavePeopleStruct错误:%w", errSavePeopleStruct)
		fmt.Println(errSavePeopleStruct)
		return errSavePeopleStruct
	}

	return nil
}

// UpdatePeopleStruct 更新
// 如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
// 如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
// 如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
// 但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdatePeopleStruct(ctx context.Context, peoplestruct *structs.PeopleStruct) error {

	// peoplestruct对象指针或主键Id不能为空
	if peoplestruct == nil || peoplestruct.Id < 1 {
		return errors.New("peoplestruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdatePeopleStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdatePeopleStruct := zorm.Update(ctx, peoplestruct)

		if errUpdatePeopleStruct != nil {
			return nil, errUpdatePeopleStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdatePeopleStruct != nil {
		errUpdatePeopleStruct := fmt.Errorf("service.UpdatePeopleStruct错误:%w", errUpdatePeopleStruct)
		fmt.Println(errUpdatePeopleStruct)
		return errUpdatePeopleStruct
	}

	return nil
}

// UpdateNotZeroPeopleStruct 更新
// 如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
// 如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
// 如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
// 但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateNotZeroPeopleStruct(ctx context.Context, peoplestruct *structs.PeopleStruct) error {

	// peoplestruct对象指针或主键Id不能为空
	if peoplestruct == nil || peoplestruct.Id < 1 {
		return errors.New("peoplestruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdatePeopleStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdatePeopleStruct := zorm.UpdateNotZeroValue(ctx, peoplestruct)

		if errUpdatePeopleStruct != nil {
			return nil, errUpdatePeopleStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdatePeopleStruct != nil {
		errUpdatePeopleStruct := fmt.Errorf("service.UpdateNotZeroPeopleStruct错误:%w", errUpdatePeopleStruct)
		fmt.Println(errUpdatePeopleStruct)
		return errUpdatePeopleStruct
	}

	return nil
}

// DeletePeopleStructById 根据Id删除
// 如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
// 如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
// 如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
// 但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func DeletePeopleStructById(ctx context.Context, id string) error {

	//id不能为空
	if len(id) < 1 {
		return errors.New("id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errDeletePeopleStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		finder := zorm.NewDeleteFinder(structs.PeopleStructTableName).Append(" WHERE id=?", id)
		_, errDeletePeopleStruct := zorm.UpdateFinder(ctx, finder)

		if errDeletePeopleStruct != nil {
			return nil, errDeletePeopleStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errDeletePeopleStruct != nil {
		errDeletePeopleStruct := fmt.Errorf("service.DeletePeopleStruct错误:%w", errDeletePeopleStruct)
		fmt.Println(errDeletePeopleStruct)
		return errDeletePeopleStruct
	}

	return nil
}

// FindPeopleStructById 根据Id查询信息
// ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindPeopleStructById(ctx context.Context, id string) (*structs.PeopleStruct, error) {
	//id不能为空
	if len(id) < 1 {
		return nil, errors.New("id不能为空")
	}

	//根据Id查询
	finder := zorm.NewSelectFinder(structs.PeopleStructTableName).Append(" WHERE id=?", id)
	peoplestruct := structs.PeopleStruct{}
	has, errFindPeopleStructById := zorm.QueryRow(ctx, finder, &peoplestruct)

	//记录错误
	if errFindPeopleStructById != nil {
		errFindPeopleStructById := fmt.Errorf("service.FindPeopleStructById错误:%w", errFindPeopleStructById)
		fmt.Println(errFindPeopleStructById)
		return nil, errFindPeopleStructById
	}

	if !has {
		return nil, nil
	}

	return &peoplestruct, nil

}

// FindPeopleStructList 根据Finder查询列表
// ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindPeopleStructList(ctx context.Context, finder *zorm.Finder, page *zorm.Page) ([]structs.PeopleStruct, error) {

	//finder不能为空
	if finder == nil {
		return nil, errors.New("finder不能为空")
	}

	peoplestructList := make([]structs.PeopleStruct, 0)
	errFindPeopleStructList := zorm.Query(ctx, finder, &peoplestructList, page)

	//记录错误
	if errFindPeopleStructList != nil {
		errFindPeopleStructList := fmt.Errorf("service.FindPeopleStructList错误:%w", errFindPeopleStructList)
		fmt.Println(errFindPeopleStructList)
		return nil, errFindPeopleStructList
	}

	return peoplestructList, nil
}
